﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LogLib;
using MetroFramework;
using ConfigManager.ChildForms;
using ComLib.ComType;
using ComLib.ConfigService;
using ProcessConfig.ConfigType;
using ProcessConfig;


namespace ConfigManager.TabPages
{
    public partial class TabPageDetectPhoto : TabPageBase
    {
        public TabPageDetectPhoto()
        {
            InitializeComponent();
        }

        private PxlAdjustOption _adjustOption = new PxlAdjustOption();

        private bool _bPxlCreat = false;

        protected IntPtr _hDetectPxl = IntPtr.Zero;

        public override void Exit()
        {
            ReleaseDetectPxlHandler();
        }

        public override void InitCtrls()
        {
            _adjustOption = Configs.ADOpts[ApplyTo.ExitAndEntry];
            QCCheckOption qcPara = Configs.QC.CRJ;
            metroToggleUseQC.Checked = qcPara.Enabled;
            metroComboBoxQCMode.SelectedIndex = (int)qcPara.UseMode;
            metroComboBoxQCEncryptType.SelectedIndex = (int)qcPara.EncryptType;
            metroToggleUseQC_CheckedChanged(null, null);
            metroToggleAdjustColor.Checked = _adjustOption.AdjustFaceColor;
            metroToggleCleanBKC.Checked = _adjustOption.CleanBKC;
            metroToggleCleanHots.Checked = _adjustOption.CleanHots;
            metroToggleCleanBKEX.Checked = _adjustOption.CleanBKEX;
            metroToggleApplyPolicy.Checked = _adjustOption.ApplyAssessPolicies;
            buttonAssessPolicy.Visible = metroToggleApplyPolicy.Checked;
           
            SetDetectQuality(Configs.TakingOption.ServiceType);

            NoSupportOption opt = Configs.NoSupportAdvice;
            metroToggleEnabled.Checked = opt.Enabled;
            textBoxZH.Text = opt.AdviceContent;
            intRangeTrackBarErrorCount.Value = opt.ErrorActivationTimes;

            metroComboBoxPxlStdType.SelectedIndex = 0;
        }


        private bool CheckInput()
        {
            if (textBoxZH.Text.Length > 18)
            {
                AutoMetroMessageBox.Show(this, "建议内容不得多于18个字", "输入有误", MessageBoxButtons.OK, MessageBoxIcon.Error, 320, 10);
                return false;
            }

            return true;
        }

        private void SetDetectQuality(ServiceTypes serType)
        {
            switch (serType)
            {
                case ServiceTypes.ExitAndEntry:
                case ServiceTypes.Optional:
                case ServiceTypes.PassportAndVisa:                  
                case ServiceTypes.IDCard:
                case ServiceTypes.PortVisa:
                case ServiceTypes.SocialSecurity:
                    break;
                default:
                    break;
            }     
        }

        //public void OnSerTypeChanged(object sender, SerTypeUpdatedEventArgs e)
        //{
        //    if (!TabPageBase.InitCompleted)
        //    {
        //        return;
        //    }
        //    SetDetectQuality(e.SerType);
        //}

        public override bool UpdateData()
        {
            if (!CheckInput())
            {
                return false;
            }
            NoSupportOption opt = Configs.NoSupportAdvice;
            opt.Enabled = metroToggleEnabled.Checked;
            opt.AdviceContent = textBoxZH.Text;
            opt.ErrorActivationTimes = intRangeTrackBarErrorCount.Value;
            Configs.NoSupportAdvice = opt;

            _adjustOption.AdjustFaceColor = metroToggleAdjustColor.Checked;
            _adjustOption.CleanBKC = metroToggleCleanBKC.Checked;
            _adjustOption.CleanBKEX = metroToggleCleanBKEX.Checked;
            _adjustOption.CleanHots = metroToggleCleanHots.Checked;
            _adjustOption.CropImage = true;
            _adjustOption.ApplyAssessPolicies = metroToggleApplyPolicy.Checked;

            Configs.ADOpts[ApplyTo.ExitAndEntry] = _adjustOption;

            QCCheckOption para3 = new QCCheckOption();
            para3.Enabled = metroToggleUseQC.Checked;
            para3.UseMode = (QCMode)metroComboBoxQCMode.SelectedIndex;
            para3.EncryptType = (EncryptMode)metroComboBoxQCEncryptType.SelectedIndex;
            Configs.QC.CRJ = para3;
            return true;
        }

        public void OnChanged(object sender, QCEventArgs e)
        {
            metroToggleUseQC.Enabled = e.EnableChanged;
            metroToggleUseQC.Checked = e.QCEnabled;
        }

        private void metroToggleUseQC_CheckedChanged(object sender, EventArgs e)
        {
            bool val = metroToggleUseQC.Checked;
            metroComboBoxQCMode.Visible = val;
            metroComboBoxQCEncryptType.Visible = val;
            label6.Visible = val;
            label7.Visible = val;
        }

        private void buttonSetPxlStd_Click(object sender, EventArgs e)
        {
            if (CreateDetectPxlHandler())
            {
                string fileKeyName = PxlFltPath.ExitAndEntry;
                int type = metroComboBoxPxlStdType.SelectedIndex;
                switch (type)
                {
                    case 0:
                        fileKeyName = PxlFltPath.ExitAndEntry;
                        break;
                    case 1:
                        fileKeyName = PxlFltPath.SFZ;
                        break;
                    case 2:
                        fileKeyName = PxlFltPath.PortVisa;
                        break;
                    case 3:
                        fileKeyName = PxlFltPath.SocialSecurity;
                        break;
                    case 4:
                        fileKeyName = PxlFltPath.PhotoPrint;
                        break;
                    case 5:
                        fileKeyName = PxlFltPath.ExitAndEntryBlue;
                        break;
                    case 6:
                        fileKeyName = PxlFltPath.SFZ_Blue;
                        break;
                    case 7:
                        fileKeyName = PxlFltPath.PortVisaBlue;
                        break;
                    case 8:
                        fileKeyName = PxlFltPath.SocialSecurityBlue;
                        break;
                    case 9:
                        fileKeyName = PxlFltPath.PhotoPrintBlue;
                        break;
                    default:
                        break;
                }
                string filePath = string.Format(@"{0}\PxlSdk\{1}.flt", Application.StartupPath, fileKeyName);
                //PxlDetect.FGDetectLoadStdDataFromFile(_hDetectPxl, filePath);
                //PxlDetect.FGDetectShowStdDataPropDlg(_hDetectPxl, true);
                //PxlDetect.FGDetectSaveStdDataToFile(_hDetectPxl, filePath);
            }
        }

        /// <summary>
        /// 释放像素算法识别句柄
        /// </summary>
        public void ReleaseDetectPxlHandler()
        {
            //int nRet = 0;
            //if (_bPxlCreat || _hDetectPxl != IntPtr.Zero)
            //{
            //    nRet = PxlDetect.FGDetectReleaseHandle(ref _hDetectPxl);
            //    _hDetectPxl = IntPtr.Zero;
            //}
        }

        /// <summary>
        /// 创建像素算法识别句柄
        /// </summary>
        public bool CreateDetectPxlHandler()
        {
            //if (_hDetectPxl != IntPtr.Zero)
            //{
            //    return true;
            //}
            //// 创建算法识别句柄         
            //int ret = PxlDetect.FGDetectCreateHandleEmpty(ref _hDetectPxl);
            //if (ret != 0)
            //{
            //    if (ret == (int)PxlDetectPINVOKE.ErrCode._FGE_LICENSE_MISMATCH)
            //    {
            //        Log.WriteAsync("PXL类库加载失败");
            //        AutoMetroMessageBox.Show(this, 
            //             "PXL句柄创建失败",
            //            "打开失败", 
            //            MessageBoxButtons.OK, 
            //            MessageBoxIcon.Error, 
            //            320, 
            //            10);
            //    }
            //    _hDetectPxl = IntPtr.Zero;
            //    return false;
            //}
            //_bPxlCreat = true;
            return true;
        }

        private void buttonAssessRequiredOption_Click(object sender, EventArgs e)
        {
            FormDetectAssessOptions form = new FormDetectAssessOptions(_adjustOption.AssessPolicies);
            if (form.ShowDialog() == DialogResult.OK)
            {
                _adjustOption.AssessPolicies = form.Option;
            }
        }

        private void metroToggleApplyPolicy_CheckedChanged(object sender, EventArgs e)
        {
            buttonAssessPolicy.Visible = metroToggleApplyPolicy.Checked;
        }

        private void metroToggleCleanBKC_CheckedChanged(object sender, EventArgs e)
        {
            if(metroToggleCleanBKC.Checked)
            {
                metroToggleCleanBKEX.Checked = false;
            }
        }

        private void metroToggleCleanBKEX_CheckedChanged(object sender, EventArgs e)
        {
            if (metroToggleCleanBKEX.Checked)
            {
                metroToggleCleanBKC.Checked = false;
            }
        }
    }
}
